<template>
  <div class="app-container">
    <!-- 添加或修改流程实例对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" v-if="open" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px"  :disabled="disabled">
        <el-form-item label="结点编码" prop="nodeCode">
          <el-input v-model="form.nodeCode" placeholder="请输入结点编码" maxlength="40" show-word-limit/>
        </el-form-item>
        <el-form-item label="结点名称" prop="nodeName">
          <el-input v-model="form.nodeName" placeholder="请输入结点名称" maxlength="100" show-word-limit/>
        </el-form-item>
        <el-form-item label="流程状态" prop="flowStatus">
          <el-select v-model="form.flowStatus" placeholder="请选择流程状态">
            <el-option
              v-for="dict in dict.type.node_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="流程版本" prop="flowVersion">
          <el-input v-model="form.flowVersion" placeholder="请输入流程版本" maxlength="20" show-word-limit/>
        </el-form-item>
        <el-form-item label="流程创建者编码" prop="userCode">
          <el-input v-model="form.userCode" placeholder="请输入流程创建者编码" maxlength="40" show-word-limit/>
        </el-form-item>
        <el-form-item label="流程创建者" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入流程创建者" maxlength="100" show-word-limit/>
        </el-form-item>
        <el-form-item label="对应flow_definition表的id" prop="definitionId">
          <el-input-number
            v-model="form.definitionId"
            placeholder="请输入对应flow_definition表的id"
            controls-position="right"
            :min="0"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" v-if="!disabled" @click="submitForm">确 定</el-button>
        <el-button @click="cancel" v-if="!disabled">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getInstance, addInstance, updateInstance } from "@/api/flow/instance";

export default {
  name: "Dialog",
  dicts: ['node_type'],
  data() {
    return {
      // 是否禁用表单
      disabled: false,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        businessId: [
          { required: true, message: "业务id不能为空", trigger: "blur" }
        ],
        nodeCode: [
          { required: true, message: "结点编码不能为空", trigger: "blur" }
        ],
        flowStatus: [
          { required: true, message: "流程状态不能为空", trigger: "change" }
        ],
        flowVersion: [
          { required: true, message: "流程版本不能为空", trigger: "blur" }
        ],
        userCode: [
          { required: true, message: "流程创建者编码不能为空", trigger: "blur" }
        ],
        userName: [
          { required: true, message: "流程创建者不能为空", trigger: "blur" }
        ],
        definitionId: [
          { required: true, message: "对应flow_definition表的id不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    debugger
    // this.open = true

    // let id = this.$route.query && this.$route.query.id;
    // let disabled = this.$route.query && this.$route.query.disabled;
    // let task = this.$route.query && this.$route.query.task;
    // if (task) {
    //   show(id, disabled)
    // }
  },
  methods: {
    /** 打开流程实例弹框 */
    async show(id, disabled) {
      debugger
      this.reset();
      this.disabled = disabled
      if (id) {
        await getInstance(id).then(response => {
          this.form = response.data;
        });
      }
      this.open = true
      if (this.disabled) {
        this.title = "详情"
      } else if (id) {
        this.title = "修改"
      } else {
        this.title = "新增"
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        businessId: null,
        nodeCode: null,
        nodeName: null,
        flowStatus: null,
        flowVersion: null,
        userCode: null,
        userName: null,
        definitionId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInstance(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.$emit('refresh');
            });
          } else {
            addInstance(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.$emit('refresh');
            });
          }
        }
      });
    },
  }
};
</script>
